package pe.com.lgutierrez.campeonato.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

@Component
public class ProcesarArchivoExcel2007 {
	
	public List<Object> leerExcel(InputStream archivoEnviado) {
		
		List<Object> cellDataList = new ArrayList<Object>();
		try{
			XSSFWorkbook workBook = new XSSFWorkbook(archivoEnviado);
			int numeroHojas = workBook.getNumberOfSheets();
			for (int j = 0; j < numeroHojas; j++) {
				XSSFSheet hssfSheet = workBook.getSheetAt(j);
				Iterator rowIterator = hssfSheet.rowIterator();
				XSSFRow hssfRow = (XSSFRow) rowIterator.next();
				while (rowIterator.hasNext()){
					hssfRow = (XSSFRow) rowIterator.next();
					Iterator iterator = hssfRow.cellIterator();
					List<Object> cellTempList = new ArrayList<Object>();
					while (iterator.hasNext()){
						XSSFCell hssfCell = (XSSFCell) iterator.next();
						cellTempList.add(hssfCell);
					}
					cellDataList.add(cellTempList);
				}
			}
		 }catch(FileNotFoundException fileNotFoundException){
             fileNotFoundException.printStackTrace();
             return null;
         }catch(IOException exception){
             exception.printStackTrace();
             return null;
         }
		
	return cellDataList;
	}

}
